package top.as.sean.api.aspect;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import top.as.sean.common.guava.GuavaCache;
import java.util.List;

/**
 * 字典翻译切面
 *
 * @author: ArchieSean
 * @create: 2022/11/6
 */
@Aspect
@Component
@RequiredArgsConstructor
public class DictAspect {

    /**
     * 本地缓存
     */
    private final GuavaCache guavaCache;

    /**
     * 定义切入点表达式
     */
    @Pointcut("@annotation(annotation.Dict)")
    public void point() {
    }

    /**
     * 环绕通知
     *
     * @param pjp pip
     * @return obj
     * @throws Throwable ex
     */
    @Around("point()")
    public Object interceptor(ProceedingJoinPoint pjp) throws Throwable {
        //执行方法
        Object proceed = pjp.proceed();
        //翻译
        if (proceed instanceof List) {
            guavaCache.translateList((List<?>) proceed);
        } else if (proceed instanceof Page) {
            guavaCache.translateList(((Page<?>) proceed).getRecords());
        } else {
            guavaCache.translateOne(proceed);
        }
        return proceed;
    }
}
